<template>
<div class="page" data-name="order-confirm">
  <div class="navbar">
    <div class="navbar-inner">
      <div class="left">
      	 <a href="#" class="link back">
           <i class="icon icon-back"></i>
         </a>
      </div>
      <div class="title">
      	确认订单
      </div>
      <div class="right">
      </div>
    </div>
  </div>
  <div class="page-content">
  	<div class="order-shipping-address-continer list">
	     <ul>
	  		<li>
		      <a href="#" class="item-link item-content">
		        <div class="item-media"><i class="iconfont icon-map"></i></div>
		        <div class="item-inner">
		          <div class="item-title">
		            <div class="item-header">
		     			 <div class="item-title-row">
				            <div class="item-title">收货人:刘瑾</div>
				            <div class="item-after">15250406101</div>
				          </div>       
		            </div>
		           	 收货地址:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
		            <div class="item-footer">(收货不便时，可代收)</div>
		          </div>
		        </div>
		      </a>
		    </li>
		 </ul>
  	</div>
  	
  	<div class="order-product-container">
				
	</div>	
  
  	<div class="confirm-container">
  		<div class="total">合计100</div>
  		<div class="btn" >
			<a  class="button button-fill button-big color-orange right confirmBtn">提交订单</a>
  		</div>
  	</div>
		
  </div>
  
</template>


<script>

function totalPrice(){
	var total = 0;
	for(var i=0;i<need_pay_goods.length;i++){
	    var payGoods = need_pay_goods[i];
	    total +=parseFloat(need_pay_goods[i]["total"]);
	}
	return total;
}

function sheetCloseEvent(){
	  $$("#view-person-tab").click();
	  $$(".bottom-bar").show();
	  router.navigate("/cart",{force:true,pushState:false,reloadAll:true});
}

  return {
    data: function () {
    	 var lotBatchListResult = [];
      return {
    	  lotBatchList: lotBatchListResult
      };
    },
  	on:{
  		 pageInit: function () {
  	        self = this;
  	        var app = self.$app;
  	        var fruits = self.fruits;
  	        router = self.$router;
  	        need_pay_goods = [];
  	        var productMap = new Map();
  	        for(var i=0;i<select_goods.length ;i++){
  	        	var products = [];
  	        	var selectProductInfo =  select_goods[i];
  	        	if(productMap.containsKey(selectProductInfo["storeId"])){//如果包含当前店铺
  	        		products = productMap.get(selectProductInfo["storeId"]);
  	        	}
  	        	var productInfo = getCartProduct(selectProductInfo["storeId"],selectProductInfo["productId"]);
  	        	products.push(productInfo);
  	        	if(!productMap.containsKey(selectProductInfo["storeId"])){
	  	        	productMap.put(productInfo["storeId"],products);
  	        	}
  	        }
  	        var keys = productMap.keys();
  	        for(var j=0;j<keys.length;j++){
  	        	var productInfoItem = productMap.get(keys[j]);
  	        	var storeName = productInfoItem[0]["storeName"];
  	        	var total = 0;
  	        	for(var t = 0;t<productInfoItem.length;t++){
  	        		total += parseFloat(productInfoItem[t]["price"])*parseInt(productInfoItem[t]["quantity"])
  	        	}
  	        	need_pay_goods.push({storeName:storeName,storeId:keys[j],total:total,products:productInfoItem});
  	        }
			  	        
  	      	var orderProductTemplate = $$('#orderProductTemplate').html();
  	    	var compiledOrderProductTemplate = Template7.compile(orderProductTemplate);
  	    	var innerHTML = compiledOrderProductTemplate({payProducts:need_pay_goods});
  	    	$$(".order-product-container").append(innerHTML);
  			
  	        
  	    	$$(".total").html("$"+totalPrice());
  	    	
  	    	
  	  		var paySheetTemplate = $$('#paySheetTemplate').html();
	    	var compiledPaySheetTemplate = Template7.compile(paySheetTemplate);
	    	var payInnerHTML = compiledPaySheetTemplate({});
  	   		// Create dynamic Sheet
  	    	var dynamicSheet = app.sheet.create({
  	    	  content: payInnerHTML,
  	    	  // Events
  	    	  on: {
  	    	    open: function (sheet) {
  	    	      console.log('Sheet open');
  	    	    },
  	    	    opened: function (sheet) {
  	    	      console.log('Sheet opened');
  	    	    },
  	    	  }
  	    	});
  	    	// Events also can be assigned on instance later
  	    	dynamicSheet.on('close', function (sheet) {
  	    	  console.log('Sheet close');
  	    	});
  	    	dynamicSheet.on('closed', function (sheet) {
  	    	  console.log('Sheet closed');
  	    	});

  	    	
  	    	
  	    	
  	    	$$(".confirmBtn").on('click',function(){
				//创建订单
  	    		 var productListResult = [];
			  	  var formData= tojsonarray("1");
			        app.request({
			          url: "js/product-list.json",
			          method: 'POST',
			          contentType: 'application/json',
			          async:false,
			          data: formData,
			          processData: true,
			          headers: {
			             Authentication: 'Bearer '+ window.localStorage.token
			          },
			          success: function (data) {
			        	  //打开支付选择
			        	  dynamicSheet.open();
			        	  //关闭事件
			        	  $$(".sheet-close").off('click');
			        	  $$(".sheet-close").on('click',sheetCloseEvent);
			        	  
			        	  $$(".alipayPayBtn").on('click',function(){
			        		  $$(".pay").on('click',function(){
			        	  	    	app.request({
			        				    url: 'http://192.168.1.106:8081/alipayResful/pay',
			        				    method: 'POST',
			        				    contentType: 'application/json',
			        				    async:false,
			        				    data: "",
			        				    timeout:5000,
			        				    success: function (data) {
			        				    	cordova.plugins.alipay.payment(data, (success) => {
			        			  	    	      console.log(success);
			        			  	    	      alert(success);
			        			  	    	    }, (error) => {
			        			  	    	      console.log(error);
			        			  	    	    });
			        				    	//$$(".pay-form").html(data);
			        				    	//$$('form[name="punchout_form"]').submit();
			        				    },
			        				    error:function (xhr,data){
			        				    	alert(xht);
			        				    }
			        				  });
			        	  	      });			        		  
			        		  
			        	  });
			        	  
			          },
			          error:function(data){
			       	   app.preloader.hide();
			       	   app.dialog.alert(data.response,function(){
				    	
			       	   });
			         }
			       });

  	    	});
  		 }
  	}
	
  };
</script>
